<?php
/*
 * Created on May 14, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

?>
<?php  $this->load->view('admin/popup/link_bar');?>
<script type="text/javascript" src="{base_url}js/jquery1.6.1.js" ></script>
<script type="text/javascript" src="{base_url}js/ajaxupload.3.5.js" ></script>

<style>
    #upload{
        margin:30px 0px; padding:5px;
        font-weight:bold; font-size:1.3em;
        font-family:Arial, Helvetica, sans-serif;
        text-align:center;
        background:#f2f2f2;
        color:#3366cc;
        border:1px solid #ccc;
        width:150px; height: 20px;
        cursor:pointer !important;
        -moz-border-radius:5px; -webkit-border-radius:5px;
    }
    .darkbg{
        background:#ddd !important;
    }
    #status{
        font-family:Arial; padding:5px;
    }
    div#files{ list-style:none; padding:0; margin:0; }
    div#files div{ padding:5px; margin-bottom:2px; width:120px; float:left; margin-right:5px;}
    div#files div img{ max-width:100px; max-height:100px; }
    .success{ background:#99f099; border:1px solid #339933; }
    .error{ background:#f0c6c3; border:1px solid #cc6622; }
</style>
<script type="text/javascript" >
    function deleteImage(image_id, image_name, consultant_id){
        var url_request = "<?php echo site_url('admin/news/delete_image/'); ?>";
        $.ajax({
            type: "POST",
            url: url_request ,
            data: "news_id=" + consultant_id + "&image_name="+ image_name ,
            dataType: "json",
            success: function(msg){
                //if(msg.error == false)
                $('#' + image_id).hide(1);
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {

                // $('#waiting').hide(500);
                // $('#message').removeClass().addClass('error').text('There was an error.').show(500);
                // $('#demoForm').show(500);
                alert( "Error: Delete image not successfull." + error.empty);
            }
        });
    }
    
    $(function(){
        var btnUpload=$('#upload');
        var status=$('#status');
        new AjaxUpload(btnUpload, {
            action: '<?php echo site_url('admin/news/do_upload/');?>/{id_news}',
            name: 'uploadfile',
            onSubmit: function(file, ext){
                if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){
                    // extension is not allowed
                    status.text('Only JPG, PNG or GIF files are allowed');
                    return false;
                }
                status.text('Uploading...');
            },
        onComplete: function(file, response){
            //On completion clear the status
            status.text(response);
            //return;
            var msg = jQuery.parseJSON(response);

                //alert(response);
                //return;
                if(response=="error"){
                    $('<li></li>').appendTo('#files').text(file).addClass('error');
                }
                else{
                    $('<div class ="success" id="'+msg.id+'"></div>').appendTo('#files').html(
                    '<img src="{base_url}data/news/nimages/'+
                        msg.filename+'" alt="" height=65 width=65 /><br />'+
                        '<a href="javascript:void();" onclick="insert(\''+msg.filename+ '\', \'vi\');">Insert (Vi)</a>' +
                      '<a href="javascript:void();" onclick="insert(\''+msg.filename+ '\', \'en\');">Insert (En)</a>' +
                        ' <a href="javascript:void();" onclick="setImageDefault(\''+msg.filename+ '\', \'{id_news}\');">Default</a>' +
                        ' <a href="javascript:void();" onclick="deleteImage('+msg.id+ ',\''+msg.filename+ '\', \'{id_news}\')">Delete</a>'
                );
                }
            //else $('<li></li>').appendTo('#files').text(response).addClass('error');
        }
    });

});
</script>


<script type = "text/javascript">

   function setImageDefault(image_name, consultant_id){
        var url_request = "<?php echo site_url('admin/news/set_image/'); ?>";
        $.ajax({
            type: "POST",
            url: url_request ,
            data: "news_id=" + consultant_id + "&image_name="+ image_name ,
            dataType: "json",
            success: function(msg){
                //if(msg.error == false)
                //    $('#' + province_id).hide(1);

            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                // $('#waiting').hide(500);
                // $('#message').removeClass().addClass('error').text('There was an error.').show(500);
                // $('#demoForm').show(500);
                alert( "Error: " + textStatus.toString() + url_request);
            }
        });
    }
    
</script>

<form action="<?php echo site_url('admin/popup/save')?>" method="post" enctype="multipart/form-data">
	<table width="747" border="0" cellpadding="0" cellspacing="0" class="listproduct" id="listproduct">
	<tr>
		<td style="padding:5px" colspan="2">
			<h3>
			<?php echo $legend_text?>
			</h3>
			<?php echo $error_message?>
		</td>
	</tr>
	
	<tr>
		<td style="padding:5px"><?php echo $this->lang->line('vi_name')?></td>
		<td>
		<input type="text" style="width: 260px; height:20px" class="text" name="txtViName" id="txtViName" value="<?php echo $vi_name?>" maxlength="100" />
		</td>
	</tr>
	<tr class="hidden">
		<td style="padding:5px"><?php echo lang('en_name')?></td>
		<td>
		<input type="text" style="width: 260px; height:20px" class="text" name="txtEnName" id="txtEnName" value="<?php echo $en_name?>" maxlength="100" />
		</td>
	</tr>
	<tr class="hidden">
		<td style="padding:5px"><?php echo lang('vi_summary')?></td>
		<td>
		<textarea class="text" name="txtViSummary" id="txtViSummary" cols="60" rows="3"><?php echo $vi_summary?></textarea>
		</td>
	</tr>
	<tr class="hidden">
		<td style="padding:5px"><?php echo lang('en_summary')?></td>
		<td>
		<textarea class="text" name="txtEnSummary" id="txtEnSummary" cols="60" rows="3"><?php echo $en_summary?></textarea>
		</td>
	</tr>
	<tr>
		<td style="padding:5px"><?php echo lang('vi_description')?></td>
		<td>
		<?php
		//echo $fckEditor->CreateHtml() ;
                echo  $this->ckeditor->editor("txtViDescription", $vi_description, $config) ;
		// echo fckEditor('txtViDescription', $news->vi_description);?>
		
		</td>
	</tr>
	<tr class="hidden">
		<td style="padding:5px"><?php echo lang('en_description')?></td>
		<td>
		<?php 
			//$fckEditor->InstanceName = 'txtEnDescription';
			//$fckEditor->Value = $news->en_description;
			//echo $fckEditor->CreateHtml() ;
			//echo fckEditor('txtEnDescription', $news->en_description);
                        echo  $this->ckeditor->editor("txtEnDescription", $en_description, $config) ;
                        ?>
		</td>
	</tr>
	
	<tr class="hidden">
		<td style="padding:5px">
		<?php echo lang('friendly_url')?> 
		</td>
		<td>
		<input type="text" class="text" name="txtFriendlyUrl" id="txtFriendlyUrl" value="<?php echo $friendly_url?>" style="width: 350px;" maxlength="60" /> <?php echo lang('invalid_characters')?>
		</td>
	</tr>
	
	<!--tr>
		<td style="padding:5px"><?php echo lang('image')?></td>
		<td>
		<?php 
		if($image!='' OR $image!=null)
			echo '<img src="'.$this->base_url.$image.'" height=90 width=90><br>';
		?>
		<input type="file" style="width: 160px; height:20px" name="file" id="file"/>
		</td>
	</tr-->
        <tr class="hidden">
                <td style="padding: 5px" colspan="2">
                    <h5>Hình ảnh:</h5>
                    <script>
                    function insert(file, location){
                       // oFormObject = document.forms['myform'];

                        //CKEDITOR.instances['instanceName'].insertHTML('&lt; img src="http://localhost/vntsolution/images/admin/add.gif"/&gt;');

                        //var imgHtml = CKEDITOR.dom.element.createFromHtml("<img align='absmiddle' src='http://localhost/vntsolution/images/admin/add.gif'>");
                        //CKEDITOR.instances.body.insertElement(imgHtml);
    
                        //oFormObject.elements["txtViDescription"].value = '<img align="absmiddle" src="http://localhost/vntsolution/images/admin/add.gif">' + oFormObject.elements["txtViDescription"].value;
                        //oFormObject.elements["txtViDescription"].value = '<img align="absmiddle" src="http://localhost/vntsolution/images/admin/add.gif">';
                        //alert("sdsd");
                        //document.getElementById("listimage").innerHTML =  '<img align="absmiddle" src="http://localhost/vntsolution/images/admin/add.gif">';
                        //var writer = new CKEDITOR.htmlWriter();
                        //writer.openTag( '<img align="absmiddle" src="http://localhost/vntsolution/images/admin/add.gif">' );
                        if(location == 'vi')
                            CKEDITOR.instances.txtViDescription.insertHtml("<img align='absmiddle' src='{base_url}data/news/nimages/"+file+"'>");
                        else
                        CKEDITOR.instances.txtEnDescription.insertHtml("<img align='absmiddle' src='{base_url}data/news/nimages/"+file+"'>");
                    }
                    </script>
                                       
                    <div id="files" >
                    {images}
                     <div class ="success" id="{image_id}"> <img src="{base_url}data/news/nimages/{vi_file_name}" alt="" /><br />
                         <a href="javascript:void();" onclick="insert('{vi_file_name}', 'vi');">Insert (Vi)</a>
                         <a href="javascript:void();" onclick="insert('{vi_file_name}', 'en');">Insert (En)</a>
                         <a href="javascript:void();" onclick="setImageDefault('{vi_file_name}', {id_news});">Default</a>
                         <a href="javascript:void();" onclick="deleteImage({image_id},'{vi_file_name}', {id_news})">Delete</a>
                    </div>
                    {/images}
                    </div>
                    
                    <div style="clear:both"></div>
                    <div id="upload" ><span>Upload File<span></div>
                                <span id="status" ></span>

                                </td>
                                </tr>
                                
	<tr>
		<td style="padding:5px"><?php echo lang('active')?></td>
		<td>
		<input type="checkbox" class="checkbox" <?php echo $active==1? 'checked' : ''?> name="active" value="1" />
		</td>
	</tr>
	<tr>
		<td></td>
		<td> 
		<?php echo lang('indicates_a_required_field');?>
 		</td>
	</tr>
	<tr>
		<td>
		</td>
		<td>
		<input type="hidden" name="txtID" id="txtID" value="<?php echo $popup_id?>"/>
<p> <input type="submit" class="submit button" name="btSave" value="<?php echo lang('save')?>" /> </p>
		</td>
	</tr>
	
</table>

</form>